﻿using System.Web;
using System.Web.SessionState;

namespace com.nm.Helper
{
    /// <summary>
    /// Session简化操作类
    /// </summary>
    public static class SessionHelper
    {
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="session"></param>
        /// <param name="pKey"></param>
        /// <returns></returns>
        public static bool IsExist(this HttpSessionState session, string pKey)
        {
            for (var i = 0; i < session.Keys.Count; i++)
            {
                if (session.Keys[i] == pKey) return true;
            }

            return false;
        }

        /// <summary>
        /// 读取
        /// </summary>
        /// <param name="session"></param>
        /// <param name="pKey"></param>
        /// <param name="dv"></param>
        /// <returns></returns>
        public static string GetString(this HttpSessionState session, string pKey, string dv = "")
        {
            return session.IsExist(pKey) ? session[pKey].ToString() : dv;
        }

        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="session"></param>
        public static void Clear(HttpSessionState session)
        {
            session.Clear();
        }

        /// <summary>
        ///
        /// </summary>
        public static HttpSessionState Session => HttpContext.Current.Session;
    }
}